/*
 * StatusBar.java  0.4.0 / Oct 26, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.swing;

import com.cute4j.swing.layouts.LayoutManager;
import com.cute4j.swing.properties.StatusBarProperties;

/**
 * Status Bar Component.
 *
 * @cute4j.native
 *    generate="header"
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public class StatusBar extends Container implements StatusBarProperties {

// Public Static Fields. -------------------------------------------------------

  /**
   * Temporary constraint (the value is {@value}).
   */
  public static final String TEMPORARY = "temporary";

  /**
   * Permanent constraint (the value is {@value}).
   */
  public static final String PERMANENT = "permanent";

// Static Initialization. ------------------------------------------------------

  static {
    _initialize();
  }

// Private Fields. -------------------------------------------------------------

  private String statusMessage;
  private boolean sizeGripEnabled;

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new StatusBar object.
   */
  public StatusBar() {
    super(null);
    sizeGripEnabled = true;
  }

// Public Methods. -------------------------------------------------------------

  /**
   * Returns current status message.
   *
   * @return current status message
   */
  public String getStatusMessage() {
    return statusMessage;
  }

  /**
   * Sets status message.
   *
   * @param message the status message
   * @param timeout the message time out
   */
  public void showMessage(String message, int timeout) {
    disposedCheck();
    _showMessage(_delegate, message, timeout);
  }

  /**
   * Sets status message.
   *
   * @param message the status message
   */
  public void showMessage(String message) {
    showMessage(message, 0);
  }

  /**
   * Clears the message.
   */
  public void clearMessage() {
    disposedCheck();
    _clearMessage(_delegate);
  }

  /**
   * Returns <tt>true</tt> if the size grip is enabled.
   *
   * @return <tt>true</tt> if the size grip is enabled
   */
  public boolean isSizeGripEnabled() {
    return sizeGripEnabled;
  }

  /**
   * Enables/disables the size grip.
   *
   * @param enabled <tt>true</tt> is enabled
   */
  public void setSizeGripEnabled(boolean enabled) {
    final boolean oldSizeGripEnabled = sizeGripEnabled;
    if (oldSizeGripEnabled != enabled) {
      disposedCheck();
      _setSizeGripEnabled(_delegate, enabled);
      sizeGripEnabled = enabled;
      firePropertyChange(PROPERTY_SIZEGRIP_ENABLED, oldSizeGripEnabled,
                         enabled);
    }
  }

// Overridden Methods. ---------------------------------------------------------

  /**
   * {@inheritDoc}
   */
  public void setLayout(LayoutManager layoutManager) {
    // empty method
  }

  /**
   * {@inheritDoc}
   */
  public void validate() {
    // empty method
  }

  /**
   * {@inheritDoc}
   */
  public void invalidate() {
    // empty method
  }

  /**
   * {@inheritDoc}
   */
  public void revalidate() {
    // empty method
  }

  /**
   * {@inheritDoc}
   */
  protected String paramString() {
    StringBuffer sb = new StringBuffer(super.paramString());
    if (statusMessage != null) {
      sb.append(",statusMessage=");
      sb.append(statusMessage);
    }
    return sb.toString();
  }

  void insertChild(Component child, Object constraints, int index) {
    if (TEMPORARY.equals(constraints)) {
      _insertChild(_delegate, child._delegate, index);
    } else {
      _insertPermanentChild(_delegate, child._delegate, index);
    }
  }

// Native Callbacks. -----------------------------------------------------------

  void _statusMessgeChanged(String message) {
    final String oldStatusMessage = statusMessage;
    if (oldStatusMessage == null) {
      if (message == null) {
        return;
      }
    } else {
      if (oldStatusMessage.equals(message)) {
        return;
      }
    }
    statusMessage = message;
    firePropertyChange(PROPERTY_STATUS_MESSAGE, oldStatusMessage, message);
  }

// Package-Private Native Methods. ---------------------------------------------

  native long _create();

  native void _showMessage(long delegate, String message, int timeout);

  native void _clearMessage(long delegate);

  native void _insertChild(long delegate, long childDelegate, int index);

  native void _insertPermanentChild(long delegate, long childDelegate, int index);

  native void _removeChild(long delegate, long childDelegate);

  native void _setSizeGripEnabled(long delegate, boolean enabled);

// Private Static Native Methods. ----------------------------------------------

  private static native void _initialize();

}
